php studies the application of [interfaces] and [polymorphism] in object orientation
- 2020-05-07 19:24:42
- OfStack
<?php
/* Interface technology
*
* Interface is 1 A special abstract class , Again, the abstract class 1 A special class
*
* Interfaces and abstract classes are 1 The role of the sample
*
* Because in the PHP Is a single inheritance, if the use of abstract class, the subclass implementation of the abstract class can no longer inherit other classes
*
* If you want to do it 1 They want to inherit from other classes. You use interfaces.
*
* Interface versus abstract classes
*
* 1. It does the same thing, you can't create an object, you have to subclass it
*
* 2. Interface declarations and abstract classes are not 1 sample
*
* 3. The interface is implemented in a way that is not 1 sample
*
* 4. All methods in an interface must be abstract methods , Only abstract methods can be declared ( Don't use abstract modified )
*
* 5. Member properties in the interface, can only declare constants, not variables
*
* 6. Member access in the interface must be public, Lowest permission in an abstract class protected
*
* Declaration interface: interface The interface name { };
*
* 7. use 1 Class to implement the interface, not to use extends, It USES implements The keyword
*
* If the subclass overrides an abstract method in the parent interface implements( implementation ), class -- Interface, abstract class -- interface use implements , the interface -- interface use extends( inheritance )
*
* You can use an abstract class to implement some of the methods in the interface
* If you want subclasses to be able to create objects, you must implement all the methods in the interface
* It is possible to define 1 Interface to inherit another 1 An interface
* 1 Classes can implement multiple interfaces ( Subclass by multiple specifications ), Separate multiple interface names with commas
* 1 Classes can be inherited 1 At the same time, to implement 1 Two or more interfaces
*
* use implements For two purposes:
*
* 1. Can implement multiple interfaces while extends Words can only be inherited 1 A parent class
*
* 2. Don't use extends Words can be inherited 1 Class, so both can be used at the same time
*
* Polymorphism: polymorphism is object-oriented 3 Of the characteristics of the large 1
*
* "Polymorphism" is an important feature of object-oriented design that exhibits dynamic binding ( dynamic binding ), also known as "namesake" ( Polymorphism ). The polymorphic functionality enables software to be developed and maintained with sufficient extensibility ( extension ). In fact, the most straightforward definition of polymorphism is that different classes of objects with inheritance can call member functions of the same name and have different effects.
*
*
*
*
*
*/
// The statement interface
interface Demo{
const HOST="localhost";
const USER="admin";
function fun1();// Declare methods without adding abstract By default. Permissions are public
function fun2();
}
// Inheritance of interface
interface Demo2 extends Demo {
function fun3();
function fun4();
}
interface Demo3{
function fun5();
function fun6();
}
interface Demo4{
function fun7();
}
echo Demo::HOST;// You can access constants in the interface
class Hello{
function fun8(){
}
}
// Subclasses must implement all methods in the interface
class UTest extends Hello implements Demo2,Demo3,Demo4 { // Implement multiple interfaces
function fun1(){
}
function fun2(){
}
function fun3(){
}
function fun4(){
}
function fun5(){
}
function fun6(){
}
function fun7(){
}
}
/*------------------- polymorphism ---------------*/
interface Test{
function fun1();
function fun2();
}
class One implements Test{
function fun1(){
echo "aaaaaaaaa";
}
function fun2(){
echo "bbbbbbbbbbbb";
}
}
class Two implements Test{
function fun1(){
echo "11111111";
}
function fun2(){
echo "2222222222";
}
}
// with 1 Interface, with the implementation 1 Methods, different objects, different output. This is the manifestation and application of polymorphism
$test=new One;
$test->fun1();// The output 1 line a
$test->fun2();// The output 1 line b
$test=new Two;
$test->fun1();// The output 1 line 1
$test->fun2();// The output 1 line 2
?>
<?php
/*-------------- Polymorphism of the 1 Application examples simulation USB Use of equipment ------------------*/
//1 a USB The interface of the
interface USB{
function mount();// loading USB The method of
function work();//USB Method of work
function unmount();// uninstall USB The method of
}
// define 1 a USB equipment U disc
class Upan implements USB{// implementation USB interface
function mount(){
echo " U disc Load the success <br/>";
}
function work(){
echo "U disc Start to work <br/>";
}
function unmount(){
echo "U disc Uninstall the success <br/>";
}
}
// define 1 a USB equipment USB The mouse
class Umouse implements USB{// implementation USB interface
function mount(){
echo " USB The keyboard Load the success <br/>";
}
function work(){
echo "USB The keyboard Start to work <br/>";
}
function unmount(){
echo "USB The keyboard Uninstall the success <br/>";
}
}
// define 1 A computer class
class Computer{
// use USB Method of equipment
function useUSB ($usb){//$usb parameter Use which USB equipment
$usb->mount();// Calling device's Loading method
$usb->work();// Calling device's Working methods
$usb->unmount();// Invokes the device's unload method
}
}
// define 1 Class of computer users
class PcUser{
// The installation USB The method of
function install(){
// First of all to 1 computer
$pc=new Computer;
// it 1 some USB equipment
$up=new Upan;// it 1 a U disc
$um=new Umouse;// it 1 a USB The mouse
// the USB Device plugged into computer , Use in computer USB Method of equipment To invoke the The device to plug in
$pc->useUSB($up);// insert U disc
$pc->useUSB($um);// insert USB The mouse
}
}
// instantiation 1 Personal computer users
$user=new PcUser;
$user->install();// Install the equipment
/*------------- output --------------
U disc Load the success
U disc Start to work
U disc Uninstall the success
USB The keyboard Load the success
USB The keyboard Start to work
USB The keyboard Uninstall the success
-----------------------------------*/
?>
Author: code name aurora
http://www.cnblogs.com/zizhuyuan/archive/2011/06/16/2082262.html